package wk.demo.fragments.dialogFragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.luoye.bzijkplayer.R;

import wk.demo.widget.wkRadioButton.SegmentedGroup;

/**
 * 独立的弹窗控件。 不依赖于 activity 的 rootView
 * 同时具有 弹窗和 fragment的优点
 */
public class WkPlayerSettingFragment extends WkPlayerBaseDialogFragment {

    private WkPlayerBaseInfoFragment wkPlayerBaseInfoFragment;
    private WkPlayerDetailSettingFragment wkPlayerDetailSettingFragment;
    private WkPlayerExtInfoFragment wkPlayerExtInfoFragment;
    private RadioGroup.OnCheckedChangeListener listener ;
    private  WkPlayerDetailSettingFragment.OnItemClick onItemClick ;;



    public static WkPlayerSettingFragment getInstance(RadioGroup.OnCheckedChangeListener listener , WkPlayerDetailSettingFragment.OnItemClick onItemClick  ) {
        WkPlayerSettingFragment fragment = new WkPlayerSettingFragment();
        fragment.setCancelable(true);
        fragment.listener = listener ;
        fragment.onItemClick= onItemClick;

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_wkcamera, container, false);
    }


    private void intSegment(View rootView , Activity activity){
        SegmentedGroup segmentedGroup =rootView. findViewById(R.id.fileInfoTypeSegment) ;
        segmentedGroup.setTintColor(Color.parseColor("#12b7f5") ,Color.parseColor("#ffffff"));
        segmentedGroup.setUnCheckedTintColor(Color.parseColor("#ffffff") , Color.parseColor("#12b7f5"));

        segmentedGroup.addButton(activity     , 1 , "基本信息") ;
        segmentedGroup.addButton(activity     , 2 , "设置");
        segmentedGroup.addButton(activity     , 3 , "实时信息");

        segmentedGroup.setOnCheckedChangeListener( listener) ;
        RadioButton first = (RadioButton) segmentedGroup.getChildAt(0);
        first.setChecked(true);
        openSwitchFragment( 1); //default
    }


    public void setMsgShow(String msg){
        if(wkPlayerBaseInfoFragment ==null){
            return;
        }
        wkPlayerBaseInfoFragment.setMsgShow(msg);
    }

    public void updateRatioInfo(String msg){
        if(wkPlayerDetailSettingFragment ==null){
            return;
        }
        wkPlayerDetailSettingFragment.updateRatioInfo(msg);
    }

    /*public void updateRenderInfo(String msg){
        if(wkPlayerDetailSettingFragment ==null){
            return;
        }
        wkPlayerDetailSettingFragment.updateRenderInfo(msg);
    }*/

    public void updateRealTimeMsg(String msg1 ,String msg2){
        if(wkPlayerExtInfoFragment ==null){
            return;
        }
        wkPlayerExtInfoFragment.updateRealTimeMsg(msg1 ,msg2);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        intSegment(view ,getActivity());
        super.onViewCreated(view, savedInstanceState);
    }

    private int rmOpenId=-1;
    public void openSwitchFragment( int index) {
        if(index==-1){ //非法id
            return;
        }
        if (rmOpenId == index) {
            return;
        }
        rmOpenId = index;
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);
        switch (index) {
            case 1:
                if (wkPlayerBaseInfoFragment == null) {
                    wkPlayerBaseInfoFragment = WkPlayerBaseInfoFragment.getInstance(   );
                    transaction.add(R.id.cameraContent, wkPlayerBaseInfoFragment);
                } else {
                    transaction.show(wkPlayerBaseInfoFragment);
                }
                break;


            case 2:
                if (wkPlayerDetailSettingFragment == null) {
                    wkPlayerDetailSettingFragment = WkPlayerDetailSettingFragment.getInstance(onItemClick    );
                    transaction.add(R.id.cameraContent, wkPlayerDetailSettingFragment);
                } else {
                    transaction.show(wkPlayerDetailSettingFragment);
                }
                break;

            case 3: //
                if (wkPlayerExtInfoFragment == null) {
                    wkPlayerExtInfoFragment = WkPlayerExtInfoFragment.getInstance(     );
                    transaction.add(R.id.cameraContent, wkPlayerExtInfoFragment);
                } else {
                    transaction.show(wkPlayerExtInfoFragment);
                }
                break;

            default:
                break;
        }
        transaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }



    private void hideAllFragment(FragmentTransaction transaction) {
        if (wkPlayerBaseInfoFragment != null) {
            transaction.hide(wkPlayerBaseInfoFragment);
        }

        if (wkPlayerDetailSettingFragment != null) {
            transaction.hide(wkPlayerDetailSettingFragment);
        }

        if (wkPlayerExtInfoFragment != null) {
            transaction.hide(wkPlayerExtInfoFragment);
        }
    }

}
